import 'package:color_dart/RgbaColor.dart';
import 'package:flutter/material.dart';
import 'package:im_flutter_sdk_example/components/a_button/index.dart';
import 'package:im_flutter_sdk_example/components/labels/index.dart';
import 'package:im_flutter_sdk_example/jsonserialize/need/data.dart';
import 'package:im_flutter_sdk_example/pages/need/widgets/active.dart';
import 'package:im_flutter_sdk_example/pages/need/widgets/head_info.dart';
import 'package:im_flutter_sdk_example/pages/need/widgets/introduce.dart';
import 'package:im_flutter_sdk_example/pages/need/widgets/main_title.dart';
import 'package:im_flutter_sdk_example/pages/news/chat_input.dart';
import 'package:im_flutter_sdk_example/pages/news/comment.dart';
import 'package:im_flutter_sdk_example/pages/toolbar/home/widgets/need_list.dart';
import 'package:im_flutter_sdk_example/provider/comment_model.dart';
import 'package:im_flutter_sdk_example/utils/global.dart';
import 'package:provider/provider.dart';

class NeedDetail extends StatefulWidget {
  @override
  _NeedDetailState createState() => _NeedDetailState();
}

class _NeedDetailState extends State<NeedDetail> {
  bool isCollect = false; //是否收藏
  bool isToComment = false; //是否点击评论
  @override
  void initState() {
    super.initState();
    Future.delayed(Duration.zero, () {
      final NeedData item = ModalRoute.of(context).settings.arguments;
      loadIsCollect(item.id.toString());
    });
  }

  Widget build(BuildContext context) {
    final NeedData item = ModalRoute.of(context).settings.arguments;
    return Scaffold(
      appBar: AppBar(title: Text('需求详情')),
      body: SafeArea(
        child: Stack(
          children: [
            Positioned(
              child: SingleChildScrollView(
                child: InkWell(
                  onTap: () {
                    cancelInputView();
                  },
                  child: Container(
                    color: rgba(249, 249, 249, 1),
                    padding: EdgeInsets.only(
                      bottom: 66,
                    ),
                    child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          HeadInfo(item: item),
                          Active(),
                          MainTitle(title: '需求描述'),
                          Introduce(item: item),
                          Comment(
                              id: item.id, type: 4, getFocus: openInputView),
                          LabelSimlarRecommend(),
                          NeedList(isRecommend: true),
                          LabelLoadAll()
                        ]),
                  ),
                ),
              ),
            ),
            Positioned(
                child: Container(
                    width: G.screenWidth(),
                    padding: EdgeInsets.only(
                        top: 8, bottom: 14, left: 16, right: 16),
                    // color: Colors.red,
                    decoration: BoxDecoration(
                        color: Colors.white,
                        boxShadow: [
                          BoxShadow(
                              color: Colors.white10, offset: Offset(0, 10)),
                        ],
                        border: Border(
                            top: BorderSide(
                                width: 1, color: rgba(235, 235, 235, 1)))),
                    child: isToComment
                        ? ChatInput(
                            type: 4,
                            id: item.id,
                            cancelInputView: cancelInputView)
                        : Container(
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.spaceAround,
                              children: [
                                Image.asset(
                                    'lib/assets/images/footer_share.png',
                                    width: 22,
                                    height: 22),
                                isCollect
                                    ? InkWell(
                                        onTap: () {
                                          cancelCollect(item.id.toString());
                                        },
                                        child: Image.asset(
                                            'lib/assets/images/footer_star_active.png',
                                            width: 22,
                                            height: 22),
                                      )
                                    : InkWell(
                                        onTap: () {
                                          addCollect(item.id.toString());
                                        },
                                        child: Image.asset(
                                            'lib/assets/images/footer_star.png',
                                            width: 22,
                                            height: 22),
                                      ),
                                InkWell(
                                  onTap: () {
                                    openInputView();
                                  },
                                  child: Image.asset(
                                      'lib/assets/images/footer_chat.png',
                                      width: 22,
                                      height: 22),
                                ),
                                AButton.normal(
                                    width: 187,
                                    child: Row(
                                      mainAxisAlignment:
                                          MainAxisAlignment.center,
                                      children: [
                                        Padding(
                                          padding:
                                              const EdgeInsets.only(left: 10),
                                          child: Text('立即报名'),
                                        ),
                                      ],
                                    ),
                                    bgColor: rgba(42, 125, 250, 1),
                                    type: 'primary',
                                    onPressed: () {}),
                              ],
                            ),
                          )),
                bottom: 0)
          ],
        ),
      ),
    );
  }

  //是否收藏
  void loadIsCollect(String id) async {
    var result = await G.req.news.reqNewsIsCollect(objType: 4, objId: id);
    print('项目是否收藏$result');
    if (result.data['status'] == 1) {
      isCollect = result.data['data'];
      this.setState(() {});
    }
  }

  //收藏
  void addCollect(id) async {
    print('$id');
    var result = await G.req.news.addNewsCollect(objId: id, objType: 4);
    print('收藏结果$result');
    if (result.data['status'] == 1) {
      G.toast('收藏成功');
      loadIsCollect(id);
    }
  }

  //取消收藏
  void cancelCollect(id) async {
    print('$id');
    var result = await G.req.news.cancelNewsCollect(objId: id, objType: 4);
    print('收藏结果$result');
    if (result.data['status'] == 1) {
      G.toast('取消收藏');
      loadIsCollect(id);
    }
  }

  //关闭输入界面
  void cancelInputView() {
    if (isToComment == true) {
      CommentModelProvider _formData =
          Provider.of<CommentModelProvider>(context);
      _formData.resetPlaceHolder();
      isToComment = false;
      this.setState(() {});
    }
  }

  //打开输入界面
  void openInputView() {
    isToComment = true;
    CommentModelProvider _formData = Provider.of<CommentModelProvider>(context);
    _formData.resetPlaceHolder();
    this.setState(() {});
  }
}
